package com.qyx.pmpucat.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.qyx.pmpucat.common.result.Result;
import com.qyx.pmpucat.common.result.Results;
import com.qyx.pmpucat.dto.UserLoginDTO;
import com.qyx.pmpucat.dto.UserLoginResponseDTO;
import com.qyx.pmpucat.dto.UserRegisterDTO;
import com.qyx.pmpucat.service.UserService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户认证控制器
 */
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
@Validated
public class AuthController {

    private final UserService userService;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result<Void> register(@RequestBody @Valid UserRegisterDTO registerDTO) {
        userService.register(registerDTO);
        return Results.success();
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<UserLoginResponseDTO> login(@RequestBody @Valid UserLoginDTO loginDTO) {
        UserLoginResponseDTO result = userService.login(loginDTO);
        return Results.success(result);
    }

    /**
     * 用户退出登录
     */
    @PostMapping("/logout")
    public Result<Void> logout() {
        StpUtil.logout();
        return Results.success();
    }

    /**
     * 获取当前登录用户ID
     */
    @GetMapping("/getUserId")
    public Result<Long> getUserId() {
        Long userId = StpUtil.getLoginIdAsLong();
        return Results.success(userId);
    }

}